package com.thb.user.dao.Impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementSetter;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.thb.user.dao.IUserInfoDAO;
import com.thb.user.model.UserInfo;

@Repository
public class UserInfoDAOImpl implements IUserInfoDAO {
	
	@Autowired
	private JdbcTemplate jtemplate;

	public int insert(UserInfo user) {
		String sql = "insert into user_info(mail,avatar,nick,gender,birth,address,sign,phone,qq,weixin,weibo,uid) values(?,?,?,?,?,?,?,?,?,?,?,?) ";
		Object[] args = new Object[]{user.getMail(),user.getAvatar(),user.getNick(),user.getGender(),user.getBirth(),user.getAddress(),user.getSign(),user.getPhone(),user.getQq(),user.getWeixin(),user.getWeibo(),user.getUid()};
		int id =  jtemplate.update(sql, args);
		return id;
	}
	

	public int update(final UserInfo user) {
		String sql = "update user_info set mail=?,avatar=?,nick=?,gender=?,birth=?,address=?,sign=?,phone=?,qq=?,weixin=?,weibo=? where uid = ?";
		Object[] args = new Object[]{user.getMail(),user.getAvatar(),user.getNick(),user.getGender(),user.getBirth(),user.getAddress(),user.getSign(),user.getPhone(),user.getQq(),user.getWeixin(),user.getWeibo(),user.getUid()};
		int id =  jtemplate.update(sql, args);
		return id;
	}


	@Override
	public int updateAll(final UserInfo user) {
		String sql = "update `user` set `sex`= ?,`college_code`= ?,`classname`= ?,`department_code`= ?,`sign`= ?,`birthday`= ?,`cellphone`= ? where `user_id` = ?";
//		Object[] args = new Object[]{userInfo.getSex(),userInfo.getCollegeCode(),userInfo.getClassName(),userInfo.getDepartmentCode(),userInfo.getSign(),userInfo.getBirthday(),userInfo.getCellPhone(),userInfo.getUserId()};
		int id = jtemplate.update(sql, new PreparedStatementSetter(){
			@Override
			public void setValues(java.sql.PreparedStatement ps)
					throws SQLException {
//				 ps.setInt(1, user.getSex());  
//                 ps.setString(2, user.getCollegeCode());
//                 ps.setString(3, user.getClassName());
//                 ps.setString(4, user.getDepartmentCode());
//                 ps.setString(5, user.getSign());
//                 ps.setLong(6, user.getBirthday()==null?0:user.getBirthday().getTime());
//                 ps.setString(7, user.getCellPhone());
//                 ps.setString(8, user.getUserId());
     
			}
			
		});
		return id;
	}
	
	private UserInfo userInfoMapper(ResultSet rs, int no) throws SQLException{
		UserInfo vo = new UserInfo();
		vo.setUid(rs.getInt("uid"));
		vo.setAvatar(rs.getString("avatar"));
		
		vo.setGender(rs.getInt("gender"));
		vo.setMail(rs.getString("mail"));
		vo.setBirth(rs.getDate("birth"));
		vo.setAddress(rs.getString("address"));
		vo.setSign(rs.getString("sign"));
		vo.setPhone(rs.getString("phone"));
		vo.setQq(rs.getString("qq"));
		vo.setWeixin(rs.getString("weixin"));
		vo.setWeibo(rs.getString("weibo"));
		return vo;
	}
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public UserInfo findInfoByUid(int uid) {
		String sql = "select * from user_info where uid = ?";
		Object[] args = new Object[]{uid};
		List<UserInfo> uList = jtemplate.query(sql, args, new RowMapper(){
			public Object mapRow(ResultSet rs, int no) throws SQLException {

				return userInfoMapper(rs, no);
			}
		});
		
		if(uList.size()==0){
			return null;
		}
		
		return uList.get(0);
		
	}

}
